import { asyncRoutes, constantRoutes } from '@/router'

const state = {
  // 路由表,标记当前用户所拥有的路由
  routes: constantRoutes // 默认静态路由
}

const mutations = {
  // otherRoutes登录成功后， 需要添加新的路由
  setRoutes(state, otherRoutes) {
    // 静态路由基础上, 累加其他权限路由
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}

const actions = {
  // 筛选路由
  filterRoutes(context, menus) {
    const otherRoutes = asyncRoutes.filter(item => {
      const name = item.children[0].name
      return menus.includes(name)
    })
    context.commit('setRoutes', otherRoutes)
    return otherRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
